package my.office.see;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.ss.usermodel.Picture;
import org.w3c.dom.Document;

public class PoiConvertOffice {

	public static void docx2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException {

	}

	public static void writeFile(byte[] data) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(new File("F:/test.html"));
			out.write(data);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void doc2html() throws TransformerFactoryConfigurationError {
		try {
			HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(new File("F:/MyEclipse10.1破解补丁/MyEclipse10.x注册机/破解说明.doc")));
			WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());

			// 这里设置生成的html中图片的路径,要和保存的图片的路径一致
			wordToHtmlConverter.setPicturesManager(new PicturesManager() {
				public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
					return "F:/MyEclipse10.1破解补丁/MyEclipse10.x注册机/pictures/" + suggestedName;
				}
			});
			wordToHtmlConverter.processDocument(wordDocument);

			// 将word中的图片保存,html页面引用
			List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
			if (pics != null) {
				for (int i = 0; i < pics.size(); i++) {
					Picture pic = pics.get(i);
					try {
						pic.writeImageContent(new FileOutputStream("F:/MyEclipse10.1破解补丁/MyEclipse10.x注册机/pictures/" + pic.suggestFullFileName()));
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
				}
			}
			Document htmlDocument = wordToHtmlConverter.getDocument();
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			DOMSource domSource = new DOMSource(htmlDocument);
			StreamResult streamResult = new StreamResult(out);
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer serializer = tf.newTransformer();
			serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			serializer.setOutputProperty(OutputKeys.INDENT, "yes");
			serializer.setOutputProperty(OutputKeys.METHOD, "html");
			serializer.transform(domSource, streamResult);
			out.close();
			writeFile(out.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

	}

}
